package com.itboye.ihomebank.activity.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.XuanZuKeAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.ZuKeBean;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.util.ByAlert;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 选租客
 */
public class ActivityXuanZuKe extends BaseOtherActivity implements Observer {
	View v_statusbar;
	XListView xlistView;
	XuanZuKeAdapter adapter;
	private List<ZuKeBean> datas;
	TextView add_shap_title_tv, nodata;
	ImageView close_icon;
	HousePresenter housePresenter;
	private String houseNo;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_xuanzuke;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("选租客");
		datas = new ArrayList<>();
		houseNo = getIntent().getStringExtra("house_no");
		adapter = new XuanZuKeAdapter(this, datas, R.layout.item_xuanzuke);
		xlistView.setAdapter(adapter);
		xlistView.setPullRefreshEnable(false);
		xlistView.setPullLoadEnable(false);
		housePresenter = new HousePresenter(this);
		if (houseNo != null) {
			if (!houseNo.equals("")) {
				housePresenter.getZuKeList(houseNo);
			}
		}
		adapter.notifyDataSetChanged();
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);

		if (entity != null) {
			if (entity.getEventType() == HousePresenter.getZuKe_success) {
				List<ZuKeBean> keBeansList = (List<ZuKeBean>) entity.getData();
				if (keBeansList != null) {
					if (keBeansList.size() >= 1) {
						datas.addAll(keBeansList);
						adapter.notifyDataSetChanged();
						xlistView.setVisibility(View.VISIBLE);
						nodata.setVisibility(View.GONE);
					} else {
						nodata.setVisibility(View.VISIBLE);
						xlistView.setVisibility(View.GONE);
					}
				}
			} else if (entity.getEventType() == HousePresenter.getZuKe_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}

}
